package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.DeleteMapping;

import java.util.List;

@Mapper
public interface AddressBookMapper {
    /**
     * 新增地址
     *
     * @param addressBook
     */
    @Insert("Insert into address_book (user_id, consignee, sex, phone, province_code," +
            " province_name, city_code, city_name, district_code, district_name," +
            " detail, label, is_default) values (#{userId}, #{consignee}, #{sex}," +
            " #{phone}, #{provinceCode}, #{provinceName}, #{cityCode}, #{cityName}, " +
            "#{districtCode}, #{districtName}, #{detail}, #{label}, #{isDefault})")
    void insert(AddressBook addressBook);

    /**
     * 条件查询
     *
     * @param addressBook
     * @return
     */
    List<AddressBook> select(AddressBook addressBook);

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook getById(Long id);

    /**
     * 根据id修改地址
     *
     * @param addressBook
     * @return
     */
    void update(AddressBook addressBook);

    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @Delete("delete from address_book where id = #{id}")
    void delete(Long id);

    /**
     * 设置默认地址
     * 将当前地址改为默认地址
     * 根据 用户id修改 是否默认地址
     *
     * @param addressBook
     * @return
     */
    @Update("update address_book set is_default = #{isDefault} where user_id = #{userId}")
    void updateIsDefaultByUserId(AddressBook addressBook);
}
